<?php





namespace app\shop\controller;



use app\admin\controller\ApiCommon;

use app\shop\model\Diagnosis as DiagnosisModel;

use app\shop\model\Goods;

use app\shop\model\Warehouse as WarehouseModel;

use app\shop\model\Goods as GoodsModel;

use app\crm\model\Customer as CustomerModel;
use app\shop\model\Refund as ModelRefund;

use Kingdee\Kingdee;

use think\response\Json;



/**

 * 诊疗单类

 * @package app\shop\controller

 * @author ZZJ

 * @create 2024-12-25

 */

class Diagnosis extends ApiCommon

{



	/**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @author Liu

     * @create 2021-10-01

     */

    public function initialize()

    {



        $action = [

            'permission' => [''],

            'allow' => ['export']

        ];

        event('CheckAuth', $action);

        $request = $this->request;

        $a       = strtolower($request->action());

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }



    /**

     * 诊疗单列表

     * @return Json

     * @author ZZJ

     * @create 2024-05-13

     */

    public function index(): Json

    {
        $header = request()->header();

        $this->param['platform'] = $header['platform'];

        $this->param['user_id'] = $this->userInfo['id'];

        $this->param['shopIds'] = $this->getCommonShopIdsCollectByUserType();

        $orderModel = new DiagnosisModel();

        $data       = $orderModel->getDataList($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data' => $data]);

    }


    /**

     * 未支付收款单列表

     * @return Json

     * @author Yang

     * @create 2022-07-14

     */

    public function unpaidList(): Json

    {

        $header = request()->header();

        $this->param['platform'] = $header['platform'];

        $this->param['user_id'] = $this->userInfo['id'];

        $this->param['shopIds'] = $this->getCommonShopIdsCollectByUserType();

        

        $orderModel = new OrderModel();

        $data       = $orderModel->getUnpaidList($this->param);

        // if (!$data) {

        //     return resultArray(['error' => $orderModel->getError()]);

        // }

        return resultArray(['data' => $data]);

    }



    /**

     * 订单列表

     * @return Json

     * @author Liu

     * @create 2021-10-01

     */

    public function refundMode(): Json

    {

        $orderModel = new OrderModel();

        $data       = $orderModel->getModeList($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data' => $data]);

    }





    /**

     * 订单主要详情

     * @return Json

     * @author Liu

     * @create 2021-10-02

     */

    public function detail(): Json

    {
        dd(2335);

        $orderModel = new DiagnosisModel();

        $data       = $orderModel->getDataDetail($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data' => $data]);

    }

	/* 订单修改 */
	public function updateDetail()
	{
	   
		$orderModel = new OrderModel();
		
	    $data       = $orderModel->updateDataDetail($this->param);
	
	    if (!$data) {
	
	        return resultArray(['error' => $orderModel->getError()]);
	
	    }
	
	    return resultArray(['data' => $data]);
	}


    /**

     * 订单商品详情

     * @return Json

     * @author Liu

     * @create 2021-10-02

     */

    public function orderGoodsDetail(): Json

    {

        $orderModel = new OrderModel();

        $data       = $orderModel->getGoodsDataDetail($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data' => $data]);

    }



    /**

     * 创建退款订单

     * @return Json

     * @author Liu

     * @create 2021-10-03

     */

    public function createOrder(): Json

    {

        $this->param['operator_id'] = $this->userInfo['id'];

        $this->param['operator_name'] = $this->userInfo['username'];

        $orderModel = new OrderModel();

        $data       = $orderModel->addData($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data'=> [], 'error' => $orderModel->getError()]);

    }



    /**

     * 创建退货订单

     * @return Json

     * @author Liu

     * @create 2021-10-03

     */

    public function createRefundGoods(): Json

    {

        $this->param['operator_id'] = $this->userInfo['id'];

        $this->param['operator_name'] = $this->userInfo['username'];

        $orderModel = new OrderModel();

        $data       = $orderModel->addGoodsData($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data'=> [], 'error' => $orderModel->getError()]);

    }





    /**

     * 修改订单状态

     * @return Json

     * @author Liu

     * @create 2021-10-08

     */

    public function checkOrder(): Json

    {

        $orderModel = new OrderModel();

        $this->param['operator_id'] = $this->userInfo['id'];

        // echo "111";
        // exit;

        $data          = $orderModel->checkOrderData($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data'=> [], 'error' => $orderModel->getError()]);

    }





    /**

     * 锁单

     * @return Json

     * @author SL

     * @create 2021-06-08

     */

    public function lockOrder(): Json

    {

        $orderModel = new OrderModel();

        $this->param['operator_id'] = $this->userInfo['id'];

        $data          = $orderModel->lcokOrderData($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data' => $data, 'error' => $orderModel->getError()]);

    }





    /**

     * 导出订单

     * @author Mr.Li

     * @create 2021-06-09 10:46

     */

    public function export()

    {

        $requestId = isset($this->request['id']) && $this->request['id'] ? $this->request['id'] : [];

        if($requestId){

            $idArr = is_array($requestId) ? $requestId : explode(',', $requestId);

        }else{

            $idArr = [];

        }

        $type=$this->request['type'];

        $orderModel = new orderModel();

        $data          = $orderModel->getDataList(['page' => 1,'limit' => 10000,'id' => $idArr,'type'=>$type]);

        if(empty($data['list'])){

            $data['list'] = [];

        }

        //收款单

        if ($type ==3)

        {

            $callName = ['deal_time' =>'成交日期','consignee' =>'客户名称','king_no' =>'客户K3代码','shop_name' => '门店','sale_name' => '护理师','refund_mode_name' => '收款方式','pay_price' => '本金','gift_price' => '赠金','pay_price' => '收款金额','business_type' => '业务类型','order_type' => '订单类型'];

            $downUrl = exportExcelData('receive_money',$callName,$data['list'],'xlsx',1);

        }elseif ($type==2)

        {

            $callName = ['deal_time' =>'成交日期','consignee' =>'客户名称','king_no' =>'客户K3代码','shop_name' => '门店','sale_name' => '护理师','goods_price' => '标准金额','total_amount' => '退货金额','business_type' => '业务类型','order_type' => '订单类型'];

            $downUrl = exportExcelData('refund_goods',$callName,$data['list'],'xlsx',1);

        }else{

            $callName = ['deal_time' =>'成交日期','consignee' =>'客户名称','king_no' =>'客户K3代码','shop_name' => '门店','sale_name' => '护理师','refund_mode_name' => '退款方式','pay_price' => '本金','gift_price' => '赠金','pay_price' => '退款金额','business_type' => '业务类型','order_type' => '订单类型'];

            $downUrl = exportExcelData('refund_money',$callName,$data['list'],'xlsx',1);

        }

        return resultArray(['data' =>['url' => 'http://'.$_SERVER['HTTP_HOST'].$downUrl],'error' => '获取成功']);

    }









    /**

     * 订单设置发货

     * @return Json

     * @author Liu

     * @create 2021-10-08

     */

    public function sendOrder(): Json

    {

        $orderModel = new OrderModel();

        $this->param['operator_id'] = $this->userInfo['id'];

        $data          = $orderModel->sendOrderData($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data'=> [], 'error' => $orderModel->getError()]);

    }



    /**

     * 订单编辑配送信息

     * @return Json

     * @author Liu

     * @create 2021-10-08

     */

    public function delivery(): Json

    {

        $orderModel = new OrderModel();

        $this->param['operator_id'] = $this->userInfo['id'];

        $data          = $orderModel->deliveryOrderData($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data'=> [], 'error' => $orderModel->getError()]);

    }



    /**

     * 订单修改金蝶单号

     * @return Json

     * @author Liu

     * @create 2021-10-08

     */

    public function editKingNo(): Json

    {

        $orderModel = new OrderModel();

        $this->param['operator_id'] = $this->userInfo['id'];

        $data          = $orderModel->editKingNoData($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data'=> [], 'error' => $orderModel->getError()]);

    }



    /**

     * 添加订单跟进

     * @return Json

     * @author Liu

     * @create 2021-10-09 10:42

     */

    public function createFollow(): Json

    {

        $this->param['operator_id'] = $this->userInfo['id'];

        $orderModel = new OrderModel();

        $data          = $orderModel->addFollow($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['error' => $orderModel->getError(), 'data'=>[]]);

    }



    /**

     * 订单跟进列表

     * @return Json

     * @author Liu

     * @create 2021-10-09 11:16

     */

    public function orderFollow(): Json

    {

        $this->param['user_id'] = $this->userInfo['id'];

        $orderModel = new OrderModel();

        $data          = $orderModel->getOrderFollowList($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data' => $data]);

    }



    /**

     * 订单操作记录

     * @return Json

     * @author Liu

     * @create 2021-10-10 9:31

     */

    public function record(): Json

    {

        $this->param['user_id'] = $this->userInfo['id'];

        $orderModel = new orderModel();

        $data          = $orderModel->getOrderRecordList($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['data' => $data]);

    }







    /**

     * 商品列表

     * @return Json

     * @author Liu

     * @create 2021-10-28

     */

    public function goodLiuist(): Json

    {

        if (!isset($this->param['user_id']))

        {

            return resultArray(['error' => '参数错误']);

        }



        $goodsModel = new GoodsModel();

        $this->param['no_page'] = 1; // 不分页

        $this->param['status'] = 1; // 上架状态

        $data = $goodsModel->getDataList($this->param);

        if (!$data) {

            return resultArray(['error' => $goodsModel->getError()]);

        }

        return resultArray(['data' => $data]);

    }



    /**

     * 客户列表

     * @param  OrderModel $orderModel

     * @return Json

     * @author Mr.Liu Wuchen

     * @create 2021-08-05 13:58

     */

    public function customerList(OrderModel $orderModel): Json

    {

        $this->param['user_id'] = $this->userInfo['id'];

        $data = $orderModel->getCustomerDataList($this->param);

        if (!$data) {

            return resultArray(['error' => $orderModel->getError()]);

        }

        return resultArray(['error' => $orderModel->getError(), 'data' => $data['data']]);

    }



    /**

     * 客户详情

     * @return Json

     * @author Liu

     * @create 2021-10-28

     */

    public function customer(): Json

    {

        if (!isset($this->param['customer_id']))

        {

            return resultArray(['error' => '参数错误']);

        }



        $customerModel = new CustomerModel();

        $data = $customerModel->getDetailsById($this->param);

        if (!$data) {

            return resultArray(['error' => $customerModel->getError()]);

        }

        // 订单类型判断

        $orderModel = new orderModel();

        $where = [];

        $where = [

            ['uid', '=', $this->param['customer_id']],

            ['status', 'in', [4,5]]

        ];

        $order = $orderModel->where($where)->count();

        $data['is_first'] = empty($order) ? 1 : 0; // 是否为首批单 0-否 1-是



        return resultArray(['data' => $data]);

    }





    /**

     * 订单退款单

     *

     */

    public function refundMoney()

    {

        $order_id = $this->param['id'];

        $kingDee = new Kingdee();

        $res = $kingDee->refund_money($order_id);

    }





    /**

     * 订单退货单

     *

     */

    public function refundGoods()

    {

        $order_id = $this->param['id'];

        $kingDee = new Kingdee();

        $res = $kingDee->refund_goods($order_id);

    }





















    /**

     * 获取销售订单数据

     *

     */

    public function getSaleOrder()

    {

        $order_id = $this->param['order_id'];

        $user_id  = $this->userInfo['id'];

        // 查询订单信息 判断订单的支付状态

        $orderModel = new orderModel();

        $orderInfo = $orderModel->where('id', $order_id)->find();

        if ( !$orderInfo ) {

            return resultArray(['error'=> '订单信息获取失败']);

        }



        // 判断客户是否确认订单

        if ($orderInfo['king_status'] != 1) {

            return resultArray(['error'=> '金蝶推送状态异常']);

        }



        // 金蝶销售出库单号

        if ( !$orderInfo['king_sale_no'] ) {

            return resultArray(['error'=> '金蝶销售出库单号为空']);

        }



        $kingDee = new Kingdee();

        $data = $kingDee->get_sale_order($orderInfo['king_sale_no']);

        return resultArray(['data'=> $data]);

    }

    /**
     * 客户数据分析表
     */
    public function getAnalysis()
    {
        $refund = new OrderModel();
        $this->param['shopIds'] = $this->getCommonShopIdsCollectByUserType();
        // print_r($this->param);
        // exit;
        $data = $refund->getAnalysis($this->param);
        return resultArray(['error' => '','data' => $data]);
    }

}



?>

